﻿using Com.Lancetinc.Evs.Helpers;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;

namespace Com.Lancetinc.Evs.Services
{
    public partial class ExperManager
    {
        public void ExportImages(ExperInfo exper, IEnumerable<string> imageNames)
        {
            var savePath = GetExportImagesSaveFilePath(exper);
            if (savePath == null)
            {
                return;
            }

            using (FileStream zipToOpen = new FileStream(savePath, FileMode.Create))
            {
                using (ZipArchive archive = new ZipArchive(zipToOpen, ZipArchiveMode.Update))
                {
                    foreach(string imageName in imageNames)
                    {
                        FileInfo imgFile = new FileInfo(exper.GetImageFullPath(imageName));
                        if (!imgFile.Exists)
                        {
                            continue;
                        }
                        using (FileStream inStream = imgFile.OpenRead())
                        {
                            ZipArchiveEntry archiveEntry = archive.CreateEntry(imageName);
                            using (Stream outStream = archiveEntry.Open())
                            {

                                inStream.CopyTo(outStream);
                            }
                        }
                    }
                }
            }
        }

        private String GetExportImagesSaveFilePath(ExperInfo exper)
        {
            SaveFileDialog dlg = new SaveFileDialog
            {
                FileName = exper.Exper.SeqNo + "_images.zip", // Default file name
                DefaultExt = ".zip", // Default file extension
                // Filter files by extension  "截图文件 (.zip)|*.zip" 
                Filter = I18nHelper.Default.GetMessage("ExportImageZipFilter") 
            };

            // Show save file dialog box
            bool? result = dlg.ShowDialog();
            if (result.HasValue && result.Value)
            {
                return dlg.FileName;
            }

            return null;
        }
    }
}
